{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b1eaf8ce-784f-4123-a268-8d3c3f3cd47e",
   "metadata": {},
   "source": [
    "1、从csv和txt文件中读取数据\n",
    "  pandas中可使用read_csv（）函数读取phones.csv文件中的数据，并指定编码格式为gbk\n",
    "  使用head（）方法指定获取phones.csv文件中前三行的数据\n",
    "  使用read_csv()函数读取itheima_books.txt文件中的数据，并指定编码格式为utf8\n",
    "  read_csv(filpath_or_buffer,sep = ',',delimiter = None,header = 'infer',names = None,index_col = None,usecols = None,squeeze = False,prefix = None,mangle+dupe_cols = True,encoding = None...)\n",
    "  filepath_or_buffer：文件路径\n",
    "  sep：分隔符，默认为逗号\n",
    "  header：表示将指定文件中的哪一行数据作为dataframe类对象的列索引，默认为0，即将第一行数据作为列索引\n",
    "  names：表示dataframe类对象的列索引列表，若文件中没有列标题，则names参数为None\n",
    "  encoding：表示指定的编码格式\n",
    "2、从excel文件中读取数据\n",
    "  pandas中可使用read_excel()函数读取athletes_info.xlsx文件，显示前五行\n",
    "3、从json文件中读取数据\n",
    "  pandas中可使用read_json()读取json文件中的数据\n",
    "  使用read_json()读取animal_species.json文件中的数据，并指定编码格式为utf8\n",
    "4、从html文件中读取数据\n",
    "5、从word文件读取数据，用python-docx库\n",
    "  python-docx库的基本使用：使用python-docx库读取“集合介绍.docx”文件中的段落内容编辑（表格内容）\n",
    "7、从pdf文件中读取，用pdfplumber库\n",
    "  使用pdfplumber库读取“集合简介.pdf”文件中所有的文本数据。可以通过page类对象中的extract_tables()方法实现只提取pdf文件中的表格数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "13e51f75-9dd0-4a29-86c2-96b419193623",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      A  B\n",
      "0     1  2\n",
      "1     2  3\n",
      "2     3  8\n",
      "3     4  5\n",
      "4     5  6\n",
      "5   560  7\n",
      "6     2  8\n",
      "7     3  9\n",
      "8     3  0\n",
      "9     4  3\n",
      "10    5  4\n",
      "11    3  5\n",
      "12    2  6\n",
      "13    4  7\n",
      "14    5  2\n",
      "15   23  4\n",
      "16    2  5\n",
      "17  342  6\n",
      "18    3  4\n",
      "\n",
      "前三行的数据为\n",
      "    A  B\n",
      "0  1  2\n",
      "1  2  3\n",
      "2  3  8\n"
     ]
    }
   ],
   "source": [
    "# 使用read_csv（）函数读取文件中的数据，并指定编码格式为gbk\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "evaluation_data = pd.read_csv('./data/example_data.csv',encoding='gbk')\n",
    "print(evaluation_data)\n",
    "\n",
    "# 使用head（）方法指定文件中前三行的数据\n",
    "print('\\n前三行的数据为\\n',evaluation_data.head(3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "318d533c-aa9a-438f-b8fc-0237a5aa456e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      A  B\n",
      "0     1  2\n",
      "1     2  3\n",
      "2     3  8\n",
      "3     4  5\n",
      "4     5  6\n",
      "5   560  7\n",
      "6     2  8\n",
      "7     3  9\n",
      "8     3  0\n",
      "9     4  3\n",
      "10    5  4\n",
      "11    3  5\n",
      "12    2  6\n",
      "13    4  7\n",
      "14    5  2\n",
      "15   23  4\n",
      "16    2  5\n",
      "17  342  6\n",
      "18    3  4\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "evaluation_data = pd.read_csv('./data/example_data.csv',encoding='utf8')\n",
    "print(evaluation_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e63acc7f-2ab6-407c-afda-96904156ccb4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     age      Male    Female\n",
      "0   100+  0.000001  0.000001\n",
      "1  95-99  0.000001  0.000001\n",
      "2  90-94  0.000001  0.001000\n",
      "3  85-89  0.002000  0.003000\n",
      "4  80-84  0.004000  0.005000\n"
     ]
    }
   ],
   "source": [
    "# 从excel中读取数据\n",
    "import pandas as pd\n",
    "excel_data = pd.read_excel('./data/age.xlsx')\n",
    "print(excel_data.head(5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b89a96a0-9bdd-42fa-a4b7-a6b62a9e6adf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   Afghanistan  Aland Islands    Albania    Algeria  American Samoa  \\\n",
      "0    67.709953      39.390897  20.168331   1.659626      -14.293662   \n",
      "1    33.939110     -99.066067  41.153332  28.033886     -170.698357   \n",
      "\n",
      "     Andorra     Angola   Anguilla  Antigua and Barbuda  Argentina  ...  \\\n",
      "0  42.545250  17.873887  18.221643            17.077530 -63.616672  ...   \n",
      "1   1.576297 -11.202692 -63.058971           -61.800068 -38.416097  ...   \n",
      "\n",
      "   Yugoslavia     Zambia   Zimbabwe      Russia  Greenland       Korea  \\\n",
      "0   51.023042  27.849332  29.154857  116.856400 -42.164707  127.766922   \n",
      "1 -116.526079 -13.133897 -19.015438   65.067703  76.422188   35.907757   \n",
      "\n",
      "   Democratic Republic of Congo  Dem. Rep. Congo   S. Sudan  \\\n",
      "0                      15.27298         15.27298  30.054890   \n",
      "1                      -4.37773         -4.37773   7.265386   \n",
      "\n",
      "   Central African Rep.  \n",
      "0              6.142800  \n",
      "1             20.399599  \n",
      "\n",
      "[2 rows x 237 columns]\n"
     ]
    }
   ],
   "source": [
    "# 从json文件中读取数据\n",
    "import pandas as pd\n",
    "json_data = pd.read_json('./data/crood_fixed.json',encoding='utf8')\n",
    "print(json_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "31eeea9b-c3f2-4641-b444-90ae7f8f1799",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   Year  Winner\n",
      "0  2023      C#\n",
      "1  2022     C++\n",
      "2  2021  Python\n",
      "3  2020  Python\n",
      "4  2019       C\n"
     ]
    }
   ],
   "source": [
    "# 从html文件中读取数据\n",
    "import requests\n",
    "\n",
    "# 获取数据\n",
    "html_data = requests.get('https://www.tiobe.com/tiobe-index/')\n",
    "\n",
    "# 读取网页中所有表格数据\n",
    "html_table_data = pd.read_html(html_data.content,encoding='utf8')\n",
    "\n",
    "# 获取索引为三的前五行表格数据\n",
    "print(html_table_data[3].head(5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "36c17c76-598e-4f43-bcec-6e1b5634cc5c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from sqlalchemy import create_engine\n",
    "engint = create_engine('mysql+pymysql://root:123456@127.0.0.1:3306/ttsx')\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
